package client;

/**
 * class Spend handles the spend CC in the system
 * 
 * @author Rasmus Stjerne, Joachim Lindholm, Daniel Gramstrup, Pamela Pedersen
 */
public class Spend extends Transaction {
	private double amount; // amount to spend
	private Input input; // reference to keypad
	double CCUSEFACTOR; // wind chill factor from yahoo.weather.com
	// constant corresponding to menu option to cancel
	private final static int CANCELED = 4;

	/**
	 * Spend constructor
	 * 
	 * @param display
	 *            to allow messages to be prompted
	 * @param connection
	 *            to allow connection to server
	 * @param input
	 *            to allow input
	 */
	public Spend(Display display, Connection connection, Input input) {
		super(display, connection);
		// gets the CCUSEFACTOR from server
		CCUSEFACTOR = connection.getCCUSEFACTOR();
		// initialize references to input
		this.input = input;
	}

	/**
	 * perform spend transaction
	 */
	@Override
	public void execute() {
		boolean transactionComplete = false;
		double availableBalance;
		Connection connection = getConnection();
		Display display = getDisplay();
		do {
			// obtain a chosen spend amount from the user
			amount = displaySpendMenu();
			// check whether user chose a spend amount or canceled
			if (amount != CANCELED) {
				// get available balance of account involved
				availableBalance = connection.viewBalance().totalBalance;
				// check whether the user has enough CC on the account
				if (amount * CCUSEFACTOR <= availableBalance) {
					// update the account
					connection.spend(amount);
					transactionComplete = true; // transaction was complete
					// Print the new balance out
					display.displayMessage("\nYour current balance is now: ");
					display.displayCCAmount((availableBalance - amount
							* CCUSEFACTOR));
					display.displayMessageLine("\n");

				} else {
					display.displayMessageLine("\nYou do NOT have enough CC to do that");
				}
			} else {
				display.displayMessageLine("\nCanceling transaction...");
				return;
			}
		} while (!transactionComplete);
	}

	/**
	 * display a menu of spend amounts and the option to cancel.
	 * 
	 * @return the chosen amount or 0 if the user chooses to cancel
	 */
	private double displaySpendMenu() {
		int userChoice = 0;
		Display display = getDisplay();
		int[] amounts = { 10, 20, 30 };
		while (userChoice == 0) {
			// display the menu
			display.displayMessageLine("\nSpend CC:");
			display.displayMessageLine("#  Price\tDescription");
			display.displaySpendLine(1, CCUSEFACTOR * amounts[0],
					"Print 10 pages");
			display.displaySpendLine(2, CCUSEFACTOR * amounts[1],
					"Recharge laptop 1 hour");
			display.displaySpendLine(3, CCUSEFACTOR * amounts[2],
					"Drive 2 km in car");
			display.displayMessageLine("\n4  Cancel transaction");
			display.displayMessage("\nChoose an amount to spend: ");
			int CC = input.getInt(1, amounts.length + 1); // get user input
															// through keypad
			// determine how to proceed based on the input value
			switch (CC) {
			case 1: // if the user chose a spend amount
			case 2: // (i.e., chose option 1, 2, 3), return the
			case 3: // corresponding amount from amounts array
				userChoice = amounts[CC - 1]; // save user's choice
				break;
			case CANCELED: // the user chose to cancel
				userChoice = CANCELED; // save user's choice
				break;
			default: // the user did not enter a value from 1-4
				display.displayMessageLine("\nInvalid selection. Try again.");
			}
		}
		return userChoice; // return userChoice or CANCELED
	}
}